<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserModel extends Model
{
    //用户表
    protected $table = 'user'; //数据表
    protected $schema=[
        'user_id' => 'int',
        'phone' => 'string',
        'password' => 'string',
        'user_name' => 'string',
        'identity' => 'int',
        'true_name' => 'string',
        'sno' => 'string',
        'ifcheck' => 'int',
        'sex' => 'string',
        'birthday' => 'date',
        'faculty' => 'string',
        'subject' => 'string',
        'grade' => 'string',
        'profile' => 'string',
        'img' => 'string',
        'city' => 'string',
        'hobby' => 'string',
        'email' => 'string',
        'credit' => 'int',
        'register_time' => 'datetime',

    ];
    protected $pk = 'user_id';  //主键
    protected $createTime = 'register_time';

    /**
     * 关联：关联登录模型
     * @return \think\model\relation\HasMany
     */
    public function loginRecord()
    {
        return $this->hasMany(LoginRecord::class,'user_id');
    }

    public static function getRecentTokenByUserid($uid)
    {
        return UserModel::where('user_id',$uid)->find()->loginRecord()->token;
    }

    public static function checkToken($user_id,$token)
    {
        $res = false;
        if($token == self::getRecentTokenByUserid($user_id))
        {
            return  true;
        }
        return $res;
    }

//    通过token获取用户id
    public static function getUseridByToken($token)
    {
        return LoginRecord::where('user_token',$token)->find()->user_id;
    }

    //    通过用户id获取用户名
    public static function getNameByUserid($user_id)
    {
        return UserModel::where('user_id',$user_id)->find()->user_name;
    }

    //    通过用户id获取手机号
    public static function getPhoneByUserid($user_id)
    {
        return UserModel::where('user_id',$user_id)->find()->phone;
    }

    //获取所有用户信息
    public static function getAllUserInfo(){

        return UserModel::order('register_time','desc')->select();

    }
}
